package com.asiainfo.iov.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.framework2.customLoading.LoadingIndicatorView;
import com.framework2.customviews.SimpleLineChart;
import com.yaxin.yyt.R;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;

public class Fragment_linechart_pre extends Fragment {
    private Context mContext;
    private View view;// infalte的布局
    private LinearLayout containerLayout;// 新建容器

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        if (null == view) {
            containerLayout = new LinearLayout(mContext);
            view = inflater.inflate(R.layout.fragment_linechart, null);
//            view.setMinimumHeight(((WindowManager) getActivity()
//                    .getSystemService(Context.WINDOW_SERVICE))
//                    .getDefaultDisplay().getHeight());
//            view.setMinimumWidth(((WindowManager) getActivity()
//                    .getSystemService(Context.WINDOW_SERVICE))
//                    .getDefaultDisplay().getWidth());
            initView();

            containerLayout.addView(view);
        } else {
            containerLayout.removeAllViews();
            containerLayout = new LinearLayout(getActivity());
            containerLayout.addView(view);
        }
        return containerLayout;
    }

    private void initView() {
        RelativeLayout loadingLayout;
        LoadingIndicatorView loadingProgress;
        TextView tvLoading;
        CardView cardView;
        loadingLayout = (RelativeLayout) view.findViewById(R.id.loadingLayout);
        loadingProgress = (LoadingIndicatorView) view.findViewById(R.id.loadingProgress);
        tvLoading = (TextView) view.findViewById(R.id.tvLoading);
        cardView = (CardView) view.findViewById(R.id.cardView);
        cardView.setVisibility(View.VISIBLE);
        loadingLayout.setVisibility(View.GONE);
        loadingProgress.setVisibility(View.GONE);
        tvLoading.setText("加载失败，点击刷新");

        DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance();
        TextView tv_total_sale = (TextView) view.findViewById(R.id.tv_total_phone_sale);
        TextView tv_store_count = (TextView) view.findViewById(R.id.tv_total_accessories_sale);
        TextView tv_left_txt = (TextView) view.findViewById(R.id.tv_left_txt);
        TextView tv_right_txt = (TextView) view.findViewById(R.id.tv_right_txt);
        tv_left_txt.setText("手机销售总量");
        tv_right_txt.setText("配件销售总量");

//        GalleryInfo info = new GalleryInfo();
//        info.setAlreadySaleCount(10 * 2);
//        info.setLastTotalSale(32 * 2);
//        info.setStoreCount(6 * 2);
//        info.setPhoneName("手机：");
//
//        decimalFormat.applyPattern("0.00");
//        tv_total_sale.setText(String.valueOf(decimalFormat.format(info.getLastTotalSale()) + "台"));
//        tv_store_count.setText(String.valueOf(info.getStoreCount() + "件"));

        SimpleLineChart mSimpleLineChart = (SimpleLineChart) view.findViewById(R.id.simpleLineChart);
        Calendar calendar = Calendar.getInstance();
        decimalFormat.applyPattern("00");
        String[] xItem = new String[7];
        int maxCount = 5;
        if (maxCount <= 8) {
            maxCount = Math.max(maxCount, 8);
        } else if (maxCount <= 16) {
            maxCount = Math.max(maxCount, 16);
        } else if (maxCount <= 40) {
            maxCount = Math.max(maxCount, 40);
        } else if (maxCount <= 80) {
            maxCount = Math.max(maxCount, 80);
        } else if (maxCount <= 200) {
            maxCount = Math.max(maxCount, 200);
        } else if (maxCount <= 400) {
            maxCount = Math.max(maxCount, 400);
        } else if (maxCount <= 800) {
            maxCount = Math.max(maxCount, 800);
        } else if (maxCount <= 2000) {
            maxCount = Math.max(maxCount, 2000);
        } else if (maxCount <= 4000) {
            maxCount = Math.max(maxCount, 4000);
        } else if (maxCount <= 8000) {
            maxCount = Math.max(maxCount, 8000);
        } else if (maxCount <= 12000) {
            maxCount = Math.max(maxCount, 12000);
        } else if (maxCount <= 20000) {
            maxCount = Math.max(maxCount, 20000);
        }
        String[] yItem = {"0", maxCount / 8 + "", maxCount / 4 + "", maxCount / 2 + "", maxCount + ""};
        for (int i = 0; i < xItem.length; i++) {
            String month = decimalFormat.format(calendar.get(Calendar.MONTH));
            String day = decimalFormat.format(calendar.get(Calendar.DAY_OF_MONTH) + 1);
            xItem[xItem.length - i - 1] = String.valueOf(month + "-" + day);
            calendar.add(Calendar.DATE, -1);//减少一天
        }

        mSimpleLineChart.setXItem(xItem);
        mSimpleLineChart.setYItem(yItem);
        //点线1-- 数据
        HashMap<Integer, Integer> pointMap1 = new HashMap();
        for (int i = 0; i < xItem.length; i++) {
            pointMap1.put(i, (int) (Math.random() * maxCount));
        }
        //点线2-- 数据
        HashMap<Integer, Integer> pointMap2 = new HashMap();
        for (int i = 0; i < xItem.length; i++) {
            pointMap2.put(i, (int) (Math.random() * maxCount));
        }
        mSimpleLineChart.setLine1Color(Color.parseColor("#FF0000"));
        mSimpleLineChart.setLine2Color(Color.parseColor("#3290e8"));
        mSimpleLineChart.setLine1BackgroundColor(Color.parseColor("#33FF0000"));
        mSimpleLineChart.setLine2BackgroundColor(Color.parseColor("#553290e8"));
        mSimpleLineChart.setData1(pointMap1);
        mSimpleLineChart.setData2(pointMap2);
    }
}
